package com.gupt.reggie.controller;/*
@author name
@data 2023/4/30-10:31
*/


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gupt.reggie.common.R;
import com.gupt.reggie.dto.OrdersDto;
import com.gupt.reggie.entity.Orders;
import com.gupt.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService ordersService;

    @PostMapping("/submit")
    public R<String> submit(HttpSession session, @RequestBody Orders order){
        log.info("用户下单数据：{}",order);
        ordersService.submit(session,order);
        return R.success("下单成功");
    }

    /**
     * 分页查询订单信息
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page<OrdersDto>> page (int page, int pageSize, String number,
                                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date beginTime,
                                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date endTime){
        Page<OrdersDto> orderPage = ordersService.page(page, pageSize, number, beginTime, endTime);
        return R.success(orderPage);
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getId,orders.getId());
        ordersService.update(orders,queryWrapper);
        return R.success("修改成功");
    }

    /**
     * 移动端订单查询
     * @param session
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> page (HttpSession session,int page, int pageSize){
        Page<OrdersDto> ordersPage = ordersService.userPage(session, page, pageSize);
        return R.success(ordersPage);
    }

    /**
     * 页面跳转
     * @param order
     * @return
     */
    @PostMapping("/again")
    public R<String> againOrder( @RequestBody Orders order){
        log.info("再来一单：{}",order.getId());
        return R.success("成功");
    }
}
